<?php
function GetChanPostFields($tid,$sage,$txt,$pic,$password,$captcha)
{
	global $args;
	$data = array(
		"board"=>$args['board'],
		"replythread"=>$tid,
		"MAX_FILE_SIZE"=>1024000,
		"em"	=> "",
		"subject"=>"",
		"email"=>($sage?"sage":""),
		"message"=>$txt,
		"postpassword"=>$password,
	);
	if ($pic)
		$data['imagefile']="@".$pic;
	else
		$data['nofile']=1;
	if ($captcha)
		$data['captcha']=$captcha;
	return $data;
}
function GetChanDeleteFields($tids,$pass)
{
	$r = "";
	foreach ($tids as $i => $tid)
	{
		$r.="delete={$tid}&";
	}
	return $r."task=delete&password=$pass";
}
function GetChanPostUrl($board)
{
	return "http://www.315chan.org/board.php";
}
function GetChanDeleteUrl($board)
{
	return "http://www.315chan.org/board.php";
}
function GetChanCaptchaUrl($tid,$board)
{
	if ($tid)
		return "http://2ch.ru/cgi-bin/captcha.pl/{$board}?key=res{$tid}&dummy={$tid}";
	else
		return "http://2ch.ru/cgi-bin/captcha.pl/{$board}?key=mainpage&dummy=";
}
function GetPageUrl($page,$board)
{
	if (!$page)
		return "http://2ch.ru/{$board}/";
	else
		return "http://2ch.ru/{$board}/{$page}.html";
}

function CheckMakePostResult($r)
{
	if (strpos($r['body'],"Вам закрыт доступ к доске. Прощайте.") OR strpos($r['body'],"403 Forbidden"))
	{
		return array(PostResult_Banned,0);
	}
	else if (preg_match("~<html><body><a href=\"/[\w]+/res/[\d]+\.[\w]+\">/[\w]+/res/([\d]+)\.[\w]+\</a></body></html>~",$r['body'],$m) OR preg_match("~Location\: /\w+/res/(\d+)\.html~",$r['header'].$r['body'],$m))
	{
		return array(PostResult_OK,$m[1]);
	}	
	else if (strpos($r['header'].$r['body'],'Post'))
	{
		return array(PostResult_OK,0);
	}
	else if (strpos($r['body'],"пароль"))
	{
		return array(PostResult_WrongPassw,0);
	}
	else if (strpos($r['body'],"уже запощен."))
	{
		return array(PostResult_PicExist,0);
	}
	else if (strpos($r['body'],"тред не существует"))
	{
		return array(PostResult_NoThread,0);
	}
	else if (strpos($r['body'],"неправильный код подтверждения"))
	{
		return array(PostResult_CaptchaWrong,0);
	}
	else if (strpos($r['body'],"код"))
	{
		return array(PostResult_Captcha,0);
	}
	else if (strpos($r['body'],"Ошибка:"))
	{
		return array(PostResult_WakabaErr,0);
	}		
	else
		return array(PostResult_UnknownErr,0);
}
function GetBumpsList($r)
{
	$pattern = "~<a href=\"/[\w\d\-]+/res/([\d]+)\.html#i([\d]+)\">[^<]+</a>\s+</span>\&nbsp\;\s+<br />\s+<span class=\"filesize\">[^<]+<a target=\"_blank\" href=\"[^\"]+\">[^<]+</a>[^<]+<em>[^<]+</em>[^<]+</span>[^<]+<span class=\"thumbnailmsg\">[^<]+</span><br />[^<]*<a target=\"_blank\" href=\"[^\"]+\">[^<]+<img src=\"[^\"]+\" width=\"[^\"]+\" height=\"[^\"]+\" alt=\"[^\"]+\" class=\"thumb\" />[^<]+</a>[^<]+<blockquote>[^<]+<p>[^<]+<br/>[^\-]+- ([\d\w\_\-]+)</p><p>[^\;]+\;[^\;]+\; Thread ID ([\d]+)/([\d]+)</p>~";
	if(preg_match_all($pattern,$r['body'],$m))
	{
		$res = array();
		foreach ($m[1] as $i => $ptid)
		{
			$res[$m[2][$i]]=array("bump"=>$m[5][$i],"parent"=>$ptid);
		}
		return $res;
	}
	else
	return 0;
}
?>